package
{
	import flash.display.Sprite;
	import Menus.MainMenu;
	import Utils.Key;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.utils.getTimer;
	
	public class TOI extends Sprite
	{
		private var gameFrameTimeLast:int = 0;
		
		private var game:Game;
		
		public function TOI()
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
			
		public function init(e:Event = null):void
		{
			Key.Listen(this.stage);
			removeEventListener(Event.ADDED_TO_STAGE, init);
			var timeDiff:int = getTimer() - this.gameFrameTimeLast;
			this.gameFrameTimeLast = getTimer();
			
			game = new Game(this);
			this.stage.addEventListener(KeyboardEvent.KEY_DOWN, game.KeyHandling);
			this.addEventListener(Event.ENTER_FRAME, this.Run);
		}
		
		public function Run(e:Event):void
		{
			var timeDiff:int = getTimer() - this.gameFrameTimeLast;
			this.gameFrameTimeLast = getTimer();
			game.Update(timeDiff);
			
		}
	}
}